package com.client.controller;

import com.client.pojo.EntryLikes;
import com.client.service.EntryLikesService;
import com.client.utils.ResponseMsg;

import jakarta.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.jsonwebtoken.Claims;
import com.client.pojo.Users;
import com.client.utils.JwtUtils;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;

/**
 * <p>
 * 词条点赞表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
@RequestMapping("/entryLikes")
public class EntryLikesController {
    @Autowired
    private EntryLikesService entryLikesService;

    /**
     * 根据用户的ID获取该用户的点赞词条历史
     * @param userId 用户ID
     * @return 词条点赞历史
     */
    @GetMapping("getLike/{userId}")
    public ResponseMsg getLike(@PathVariable Integer userId){
        List<EntryLikes> entryLikes = entryLikesService.getByIds(userId);
        if(entryLikes == null){
            return ResponseMsg.getInstance(ResponseMsg.ERROR,"暂无点赞历史~");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"查询点赞历史~",entryLikes);
    }

    //根据用户ID和词条ID查询是否已经点赞
    @PostMapping("/searchIfLiked")
    public String searchIfLiked(@RequestBody Map<String, Object> param,HttpServletRequest request) {
        Integer userId = (Integer) param.get("userId");

        Integer entryId = (Integer) param.get("entryId");
        if(entryLikesService.searchIfLiked(userId,entryId)){
            return JSON.toJSONString(true);
        }
        return JSON.toJSONString(false); 
    }
    //根据用户ID和词条ID,作者ID增加点赞
    @PostMapping("/addLikes")
    public String addLikes(@RequestBody Map<String, Object> param,HttpServletRequest request){
        Integer userId = (Integer) param.get("userId");
        Integer entryId = (Integer) param.get("entryId");
        Integer authorId = (Integer) param.get("authorId");
        entryLikesService.addLikes(userId,entryId,authorId);

        return JSON.toJSONString(true);
    }
    //根据用户ID和词条ID取消点赞
    @PostMapping("/cancelLikes")
    public String cancelLikes(@RequestBody Map<String, Object> param,HttpServletRequest request){
        Integer userId = (Integer) param.get("userId");
        Integer entryId = (Integer) param.get("entryId");
        entryLikesService.cancelLikes(userId,entryId);
        
        return JSON.toJSONString(true);
    }
}
